package ynu.lcy.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import ynu.lcy.dto.order.AddOrderRequestDTO;
import ynu.lcy.elememicroservicecommon.utils.BaseResponse;
import ynu.lcy.elememicroservicecommon.utils.ResultUtils;
import ynu.lcy.elememicroserviceserviceclient.service.BusinessFeignClient;

import ynu.lcy.entity.Business;
import ynu.lcy.entity.Order;
import ynu.lcy.service.OrderService;
import ynu.lcy.vo.BusinessVO;
import ynu.lcy.vo.OrderDetailVO;
import ynu.lcy.vo.OrderVO;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/")
@CrossOrigin
@Api(tags = "订单接口")
public class OrderController {
    @Resource
    private OrderService service;

    @Resource
    private ModelMapper modelMapper;

    @Resource
    private BusinessFeignClient businessFeignClient;

    /**
     * 创建订单
     *
     * @param dto 订单信息
     */
    @PostMapping("/create")
    @ApiOperation("创建订单")
    public BaseResponse<Long> create(@RequestBody AddOrderRequestDTO dto, HttpServletRequest request) {
        Order order = service.create(dto, request);
        if (Objects.isNull(order)) return ResultUtils.success(null);
        OrderVO vo = objToVo(order);
        return ResultUtils.success(vo.getId());
    }

    /**
     * 更新订单
     *
     * @param id 订单id
     * @return 订单信息
     */
    @GetMapping("/update/{id}")
    @ApiOperation("更新订单")
    public BaseResponse<OrderVO> update(@PathVariable("id") Long id) {
        Order order = service.payOrder(id);
        if (Objects.isNull(order)) return ResultUtils.success(null);
        OrderVO vo = objToVo(order);
        return ResultUtils.success(vo);
    }

    /**
     * 获取订单
     *
     * @param id 订单id
     * @return 订单信息
     */
    @GetMapping("/get/{id}")
    @ApiOperation("获取订单")
    public BaseResponse<OrderVO> get(@PathVariable("id") Long id) {
        Order order =service.getOrderByOrderId(id);
        if (Objects.isNull(order)) return ResultUtils.success(null);
        OrderVO vo = objToVo(order);
        return ResultUtils.success(vo);
    }

    /**
     * 获取订单列表
     *
     * @param request 请求
     * @return 订单信息
     */
    @GetMapping("/list")
    @ApiOperation("获取订单列表")
    public BaseResponse<List<OrderVO>> list(HttpServletRequest request) {
        List<Order> orders =service.list(request);
        if (Objects.isNull(orders)) return ResultUtils.success(null);
        List<OrderVO> vos = new ArrayList<>();
        orders.forEach(order ->vos.add(objToVo(order)));
        return ResultUtils.success(vos);
    }

    /**
     * 获取商家订单
     *
     * @param id id
     * @return 订单信息
     */
    @GetMapping("/getByBusinessId/{businessId}")
    @ApiOperation("获取商家订单")
    public BaseResponse<List<OrderVO>> getByBusinessId(@PathVariable("businessId") Long id) {
        List<Order> orders =service.list(id);
        if (Objects.isNull(orders)) return ResultUtils.success(null);
        List<OrderVO> vos = new ArrayList<>();
        orders.forEach(order ->vos.add(objToVo(order)));
        return ResultUtils.success(vos);
    }


    private OrderVO objToVo(Order order) {
        OrderVO vo = modelMapper.map(order, OrderVO.class);
        Business business = businessFeignClient.getById(order.getBusinessId());

        List<OrderDetailVO> vos =  new ArrayList<>();

        order.getOrderDetails().forEach(orderDetail ->{
            vos.add(modelMapper.map(orderDetail, OrderDetailVO.class));
        });
        BusinessVO businessVO = modelMapper.map(business, BusinessVO.class);
        vo.setBusinessVO(businessVO);
        vo.setOrderDetailVOS(vos);
        return vo;
    }
}
